package com.uxwine.view.t;

import com.uxwine.alarm.R;
import com.uxwine.util.Log;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewParent;
import android.widget.ImageView;
import android.widget.Toast;

public class BtnLayer extends ImageView {

	private static final String TAG = BtnLayer.class.getName();
	BitmapDrawable mBitmap;

	public BtnLayer(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}	

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if(event.getAction() == MotionEvent.ACTION_DOWN)
		{
			if(isIgnoreEvent((int)event.getX(), (int)event.getY()))
				return false;
		}
		return super.onTouchEvent(event);
	}	
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if(null != mBitmap){
			int w = getWidth();
			int h = getHeight();
			int cx = (int)(getWidth()*0.8f);
			int cy = (int)(getHeight()*0.8f);
			
			int l = (w - cx) / 2;
			int t = (h - cy) / 2;
			mBitmap.setBounds(l, t, l + cx, t + cy);
			mBitmap.draw(canvas);
		}
	}

	public void setBitmap(int resId)
	{
		mBitmap = (BitmapDrawable)getResources().getDrawable(resId);
	}

	
	protected boolean isIgnoreEvent(int x, int y)
	{
		StateListDrawable sld = (StateListDrawable)getDrawable();
		BitmapDrawable bd = (BitmapDrawable)sld.getCurrent();
		Bitmap bm = bd.getBitmap();
		if(null == bm)
			return true;
		try {
			return (bm.getPixel(x, y) == 0);
		} catch (Exception e) {
			return true;
		}
	}
	
	private void initView(Context context) {
		setClickable(true);
		setBitmap(R.drawable.time_dial_year);
	}
}
